package com.zhuge_new.untils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zhuge_new.R;

/**
 * Created by Administrator on 2018/11/8.
 */

public class GlideUtils {

    private static Bitmap bitmap;

    /**
     *   普通加载图片
     *
     */

    public static void loadClippingImageSize(Context context, String url,int width,int hight, ImageView imageView){
        RequestOptions options=new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .override(width,hight)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).load(url).apply(options).into(imageView);
    }


    public static void loadImage(Context context, String url, ImageView imageView){
        RequestOptions options=new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).load(url).apply(options).into(imageView);
    }

    /**
     * 返回bitmap
     */
    public static Bitmap loadBitmap(Context context,String url ){

        RequestOptions options=new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context).asBitmap().load(url).apply(options).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                bitmap = resource;
            }
        });
        return bitmap;
    }

    /**
     * 圆角
     */
    public static void loadcircle(Context context, String url, ImageView imageView,int a,int b,int c){
        //设置图片圆角角度
        RoundedCorners roundedCorners= new RoundedCorners(a);
       //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options=RequestOptions
                .bitmapTransform(roundedCorners)
                .override(b,c);

        Glide.with(context).load(url).apply(options).into(imageView);

    }

}
